package hamsterpolice.todo;


import hamsterpolice.todo.data.DataNotFoundException;
import hamsterpolice.todo.model.Location;
import hamsterpolice.todo.model.ToDoItem;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.ItemizedOverlay;


public class GoogleMapsActivity extends MapActivity{
	
	private final static String TAG = "GoogleMapsActivity";
	private final static String ITEM_IDS = "ITEM_IDS";
	private ToDoItemizedOverlay itemizedoverlay;
	private List<ToDoItem> items = null;
	private List<Overlay> mapOverlays = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.googlemapslayout);
	    MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    Bundle extras = getIntent().getExtras();
	    long[] itemIds = extras.getLongArray(ITEM_IDS);
	    items = new ArrayList<ToDoItem>();
	    
	    for (int i=0; i<itemIds.length; i++) {
	    	try {
	    		items.add(ToDoItem.get(this, itemIds[i]));
	    	} catch (DataNotFoundException e) {
	    		Log.e(TAG, "Data Not Found!");
	    	}
	    }
	    
	    mapOverlays = mapView.getOverlays();
	    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
	    itemizedoverlay = new ToDoItemizedOverlay(drawable, this);
	    
	    //---------TEST CODE
	    /*ToDoItem item = new ToDoItem(this);
	    item.setName("Test Name");
	    item.setDescription("Test Description");
	    Location loc = new Location("Location Description", 33.4456, -84.2317); //Coordinates for Atlanta, GA
	    item.setLocation(loc);
	    items.add(item);*/
	    //--------------------------------
	    
	    showItems();
	}
	
	private void showItems() {
		for (ToDoItem item : items) {
			Location l = item.getLocation();
			if (l!=null) {
				GeoPoint point = new GeoPoint((int)(l.getLatitude()*1000000), (int)(l.getLongitude()*1000000));
				OverlayItem overlayitem = new OverlayItem(point, item.getName(), l.getDescription());
				itemizedoverlay.addOverlay(overlayitem);
			}
		}
		mapOverlays.add(itemizedoverlay);
	}
	
	protected  boolean isRouteDisplayed() {
		return false;
	}
	
	private class ToDoItemizedOverlay extends ItemizedOverlay {
		
		private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
		private Context mContext;
		
		public ToDoItemizedOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
		}
		
		public ToDoItemizedOverlay(Drawable defaultMarker, Context context) {
			  super(boundCenterBottom(defaultMarker));
			  mContext = context;
		}
		
		@Override
		protected OverlayItem createItem(int i) {
			  return mOverlays.get(i);
		}
		
		public void addOverlay(OverlayItem overlay) {
		    mOverlays.add(overlay);
		    populate();
		}
		
		protected boolean onTap(int index) {
			  OverlayItem item = mOverlays.get(index);
			  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			  dialog.setTitle(item.getTitle());
			  dialog.setMessage(item.getSnippet());
			  dialog.show();
			  return true;
		}
		
		@Override
		public int size() {
			  return mOverlays.size();
		}
		
	}
	
}
